package com.jiang.vo;

import java.io.Serializable;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class Member implements Delayed, Serializable{

	private static final long serialVersionUID = 1L;
	private String name;
	private long delay; // 延迟的时间段
	private long expire; // 离开的时间点
	
	public Member(String name, long delay, TimeUnit time) {
		this.name = name;
		this.delay = TimeUnit.MILLISECONDS.convert(delay, time); //统一转换为毫秒
		this.expire = System.currentTimeMillis() + this.delay;
	}

	/**
	 * 出队列的优先级考虑
	 */
	@Override
	public int compareTo(Delayed o) {
		
		return (int) (delay - getDelay(TimeUnit.MILLISECONDS));
	}

	/**
	 * 延迟时间是否到了的考虑
	 */
	@Override
	public long getDelay(TimeUnit unit) {
		return unit.convert(this.expire-System.currentTimeMillis(), TimeUnit.MILLISECONDS);
	}

	@Override
	public String toString() {
		
		return "到了出队列的时间"+this.name;
		
	}
	
}
